home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_360 / uucp / uucp0.lzh / src / unix / uudecode.c < prev    next >
C/C++ Source or Header  |  1990-05-17  |  3KB  |  219 lines

  1.  
  2. /*
  3.  *  UUDECODE.C
  4.  *
  5.  *  $Header: Beta:src/uucp/src/compress/RCS/uudecode.c,v 1.1 90/02/02 11:48:08 dillon Exp Locker: dillon $
  6.  *
  7.  * uudecode [input]
  8.  *
  9.  * create the specified file, decoding as you go.
  10.  * used with uuencode.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include "version.h"
  15.  
  16. IDENT(".00");
  17.  
  18. #ifdef UNIX
  19. # include <pwd.h>
  20. # include <sys/types.h>
  21. # include <sys/stat.h>
  22. #endif
  23.  
  24. #ifdef VMS
  25. # include <types.h>
  26. # include <stat.h>
  27. #endif
  28.  
  29. /* single character decode */
  30. #define DEC(c)  (((c) - ' ') & 077)
  31.  
  32. void outdec();
  33. void decode();
  34. void xerror();
  35.  
  36. void
  37. main(argc, argv)
  38. char **argv;
  39. {
  40.     FILE *in, *out;
  41. #ifdef UNIX | VMS
  42.     struct stat sbuf;
  43. #endif
  44.     int mode;
  45.     char dest[128];
  46.     char buf[80];
  47.  
  48.     /* optional input arg */
  49.     if (argc > 1) {
  50.         if ((in = fopen(argv[1], "r")) == NULL) {
  51.             xerror(argv[1]);
  52.             exit(1);
  53.         }
  54.         argv++; argc--;
  55.     } else
  56.         in = stdin;
  57.  
  58.     if (argc != 1) {
  59.         printf("Usage: uudecode [infile]\n");
  60.         exit(2);
  61.     }
  62.  
  63.     /* search for header line */
  64.     for (;;) {
  65.         if (fgets(buf, sizeof buf, in) == NULL) {
  66.             fprintf(stderr, "No begin line\n");
  67.             exit(3);
  68.         }
  69.         if (strncmp(buf, "begin ", 6) == 0)
  70.             break;
  71.     }
  72.     sscanf(buf, "begin %o %s", &mode, dest);
  73.  
  74. #ifdef UNIX
  75.     /* handle ~user/file format */
  76.     if (dest[0] == '~') {
  77.         char *sl;
  78.         struct passwd *getpwnam();
  79.         char *index();
  80.         struct passwd *user;
  81.         char dnbuf[100];
  82.  
  83.         sl = index(dest, '/');
  84.         if (sl == NULL) {
  85.             fprintf(stderr, "Illegal ~user\n");
  86.             exit(3);
  87.         }
  88.         *sl++ = 0;
  89.         user = getpwnam(dest+1);
  90.         if (user == NULL) {
  91.             fprintf(stderr, "No such user as %s\n", dest);
  92.             exit(4);
  93.         }
  94.         strcpy(dnbuf, user->pw_dir);
  95.         strcat(dnbuf, "/");
  96.         strcat(dnbuf, sl);
  97.         strcpy(dest, dnbuf);
  98.     }
  99. #endif /* UNIX */
  100.  
  101.     /* create output file */
  102.     out = fopen(dest, "w");
  103.     if (out == NULL) {
  104.         xerror(dest);
  105.         exit(4);
  106.     }
  107. #ifdef UNIX | VMS
  108.     chmod(dest, mode);
  109. #endif
  110.  
  111.     decode(in, out);
  112.  
  113.     if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
  114.         fprintf(stderr, "No end line\n");
  115.         exit(5);
  116.     }
  117.     exit(0);
  118. }
  119.  
  120. /*
  121.  * copy from in to out, decoding as you go along.
  122.  */
  123.  
  124. void
  125. decode(in, out)
  126. FILE *in;
  127. FILE *out;
  128. {
  129.     char buf[80];
  130.     char *bp;
  131.     int n;
  132.  
  133.     for (;;) {
  134.         /* for each input line */
  135.         if (fgets(buf, sizeof buf, in) == NULL) {
  136.             printf("Short file\n");
  137.             exit(10);
  138.         }
  139.         n = DEC(buf[0]);
  140.         if (n <= 0)
  141.             break;
  142.  
  143.         bp = &buf[1];
  144.         while (n > 0) {
  145.             outdec(bp, out, n);
  146.             bp += 4;
  147.             n -= 3;
  148.         }
  149.     }
  150. }
  151.  
  152. /*
  153.  * output a group of 3 bytes (4 input characters).
  154.  * the input chars are pointed to by p, they are to
  155.  * be output to file f.  n is used to tell us not to
  156.  * output all of them at the end of the file.
  157.  */
  158.  
  159. void
  160. outdec(p, f, n)
  161. char *p;
  162. FILE *f;
  163. {
  164.     int c1, c2, c3;
  165.  
  166.     c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
  167.     c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  168.     c3 = DEC(p[2]) << 6 | DEC(p[3]);
  169.     if (n >= 1)
  170.         putc(c1, f);
  171.     if (n >= 2)
  172.         putc(c2, f);
  173.     if (n >= 3)
  174.         putc(c3, f);
  175. }
  176.  
  177.  
  178. /* fr: like read but stdio */
  179. int
  180. fr(fd, buf, cnt)
  181. FILE *fd;
  182. char *buf;
  183. int cnt;
  184. {
  185.     int c, i;
  186.  
  187.     for (i=0; i<cnt; i++) {
  188.         c = getc(fd);
  189.         if (c == EOF)
  190.             return(i);
  191.         buf[i] = c;
  192.     }
  193.     return (cnt);
  194. }
  195.  
  196. /*
  197.  * Return the ptr in sp at which the character c appears;
  198.  * NULL if not found
  199.  */
  200.  
  201. char *
  202. index(sp, c)
  203. register char *sp, c;
  204. {
  205.     do {
  206.         if (*sp == c)
  207.             return(sp);
  208.     } while (*sp++);
  209.     return(NULL);
  210. }
  211.  
  212. void
  213. xerror(err)
  214. char *err;
  215. {
  216.     printf("Can not open file \"%s\"\n", err);
  217. }
  218.  
  219.